package com.lw.mall.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lw.mall.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121616248";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCR1r03mhXn2WQCfDCIUr5GTItq0cAvd3kIqSfR2SD1kPZTgisYr4I3UZHaFC1r+sQZl8CENgkeF96ixkeROIr6aUh47nh2S5Jszs5xg/RgchkT3kl4G2+OrJm1MVvSERQVBKYgZDxyFjI6Xfyd1aMZX9lpiQ9JT7SX+a4I8C9t4Q8HKdQ9yGS76AT5xhohXBXyNhdNIGIweYuCRrJIoNjlwPE+xuAjyPuQkUWkM6yr3rddvnTvXY86yd1d/TEkURY8romBghyC7KJW/czvoEbX8BzKcu5O+yC3l7SQs9vDjlDjqHyONokEAU6EkI+uNiiPZwO4nQobZ+Y0THs63vYBAgMBAAECggEAfcMjasZaa5QA9U+o/nX0FWU+uL/mLkilu/07FHBcRsdaVAslJOV8sj2KO/g4byxRXQyK/LgoRqYo3E+GOcn7txOBZp+mi8SBoYj5Z9R1sZHS5FpFGuj126Al8KV+2/jUS4e1xqsJoQxZBhI0nNs1CdWpA6Qo5XxxbLjoBlcxX1tt4nXp+YYmnZJb6EzDJAsQNKMMfORtceL09ib2MaiOS1Hh2OxenFG37B1lZlPeQThXplIua76XtlLANAouMeLlGipb30oUvdUZE9iOeLTiGIMEmbqyKMwOrqzGtyYfFAJycvv7/dU5R8iE5s3+19q+AAipiwhjyN3KMm7Ln9oHEQKBgQDPj4jAYjMUE6vRueqhwj6LbT/R6tiKbFbji5g+VvxJHBzA47gctcxX0S4G5L1VRsL4DN1drmi2RMRZnjBZxwXoQItTN3+xUYpKpnGyRzMxZq4oPBERAscxZIhRDnGEyrjgUvVQ4DkU7R8Ic0bIw6LRjSYpOQqv0MklJlrqrxyxXQKBgQCz37TFYvR0Bbh+tBDHcbDpWft9H31TBjo5pkvm0hScPGzJ/ij/VlcI3vfBfxfPvdNl/rJG3dKE0icYkj4VoDYpWu87lyyfWV1Yu1NEhVdnuVI9Q8HrMoJPslCIuvRZghHg3F9NUYzPp3Yj883rzGLxyHR6CKEX6TtjhvvQs+KY9QKBgFsEw2Ax79o3Ahq3H5gNuSWlI95m0Mxq0/N2rK1xsbJRvJ4tffhBFLnRxhBJ0GkL3cWuxoglSk/begsenm9QIlwFqdM1PC7C5hT0Rll6dY2NTIPiS02dXUJgppJ89fTYfRgJ6P+tQ/rUPM6U1o6Fk+m2ruxW0kPlF7/NwQK3BasVAoGAaOsc02HD+wT3N5iqjNtI+OQOqE482LeyMLxKW6nAcd1hC3Vfzog7HiaIV2pdpkQwUbb9oA51ZBn8yf1Lj2uzC+5JyvWEBrKmm0Iw8K/iatMTID8slpcQoZH+HbMHIkqXSRTh1gSyYNFqPNBSFa9TK3rkTeZIqNzXUleC/Jpgmx0CgYBnLalq+Zdp1oaCCcNzU942guHblDl+nMGT1W/emKe+ZvNwN5roxXEv1Jrfq3mpFxvAfuGEQ/gMIJgTI6E+LxknNL7ihAuvMe0d45zJojkoVXsD65aSBnI60Y+alsYhcG6vQ+HbZdNzY08hX8ZnTQ6ZbQCzSzka7KUJc6aJtGJ5pA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzIMCX/Cyox3JH8D7xIi6CECn3Xo7b8US1Pv1T09+dPKODfDoAfnkBimhUj8iVT4uoWRqI5+ecJQRz3a/ZiOWnOe9le+Yp9L7LCJJJ2vPGqiHfcuJCfdcY8+EyRDSO48CohrECwGXFbZHgMK83xOyPLaezQp4akQd8Hqjxefl0GVYjMTd6xxrM0ljsBkpw4OLsv5VDeVk3r1OE90DycXxXjbKWDkp1M/K9H/L17oHXfCBcJeIPiblb9MXbsv/qECnR3YcWGjNJudPmw+EoOXr1i/Chn2toh4xZZ393RT8ZX0jnrBXWD+8Cu8SbXDq8FuQ4QmJOGJPOEeELHYO0WmAKQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息 在公网
    public static String notify_url = "http://dor0fw1nn0.54http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问

    //同步通知，支付成功，一般跳转到成功页
    public static String return_url = "http://localhost:8081/mall/order/toList.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    //30m 支付宝自动关单
    private String timeout = "30m";
    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}